// pages/netCase/court/court.js
import ecfsConfig from '../../../config/ecfs.config.js'
import locale from '../../../config/locale/zh-CN/netCase/court.js'
/**picker***/
var model = require('../../../component/area/model.js')
var show = false;
var item = {};
/**picker***/
Page({

  /**
   * 页面的初始数据
   */
  data: {
    courtIndex: '',
    caseId: '',
    courtId: '',
    courtName: '',
    locale,
    selected: false,
    cityCode: [],
    countyCode: [],
    provinceCode: [],
    item: {
      show: show
    },
    address: '',
    courtList: [],
  },
  /**picker***/
  /**
 * 输入框获取焦点，隐藏pickview
 */
  onFacus: function (e) {
    this.hiddenFloatView();
  },
  /**
   * 选择常住地
   */
  selectJuZhu: function (e) {
    this.pickup();
  },
  pickup: function (e) {
    var that = this;
    model.pickup(this, "", "", "", function (result) {
      that.setData({
        item: result
      });
    })
  },
  //取消或者 隐藏picker-view
  hiddenFloatView: function (e) {
    var that = this;
    model.pickDown(this, function (result) {
      that.setData({
        item: result
      });
    })
  },
  //选取了值，再隐藏view
  okFloatView: function (e) {
    let that = this;
    model.pickDown(this, function (result) {
      that.setData({
        item: result
      });
      var value = result.value;
      var sheng = result.provinces[value[0]];
      var shi = result.citys[value[1]];
      var xian = result.countys[value[2]];
      var provinceArray = [sheng.value]
      var cityArray = [shi.value]
      var countyArray = [xian.value]
      that.setData({
        'provinceCode': provinceArray,
        'countyCode': countyArray,
        'cityCode': cityArray,
        'address': sheng.label + shi.label + xian.label
      })
    })
    wx.request({
      url: ecfsConfig.IS_PRODUCTION ? (ecfsConfig.SERVER_DOMAIN + ecfsConfig.SELECT_COURT_URL) : (ecfsConfig.SERVER_DOMAIN + ecfsConfig.RAP_URL_PREFIX + ecfsConfig.SELECT_COURT_URL),
      data: {
        cityCode: that.data.cityCode,
        countyCode: that.data.countyCode,
        provinceCode: that.data.provinceCode
      },
      method: 'POST',
      success: function (res) {
        if (res.data.code === 200) {
          that.setData({
            courtList: res.data.body.result
          })
        }
      },
      fail: function (res) {
        console.log(res.data);
        console.log('is failed')
      }
    })
  },
  //滑动事件
  bindChange: function (e) {
    var that = this;
    model.pickScroll(e, this, function (result) {
      that.setData({
        item: result
      });

    });
  },
  selectCourt(e) {
    var courtId = e.currentTarget.dataset.courtid,
      courtName = e.currentTarget.dataset.courtname,
      index = e.currentTarget.dataset.index
    this.setData({
      courtId: courtId,
      courtName: courtName,
      selected: true,
      courtIndex: index
    });
  },
  cancel() {
    wx.navigateTo({
      url: '../evidenceList/evidenceList'
    })
  },
  submit() {
    if (this.data.courtName != '') {
      let that = this
      wx.request({
        url: ecfsConfig.IS_PRODUCTION ? (ecfsConfig.SERVER_DOMAIN + ecfsConfig.SAVE_COURT_URL) : (ecfsConfig.SERVER_DOMAIN + ecfsConfig.RAP_URL_PREFIX + ecfsConfig.SAVE_COURT_URL),
        data: {
          caseId: that.data.caseId,
          courtName: that.data.courtName,
          id: that.data.courtId
        },
        method: 'POST',
        success: function (res) {
          if (res.data.code === 200) {
            //跳到案件详情
          }
        },
        fail: function (res) {
          console.log(res.data);
          console.log('is failed')
        }
      })
    } else {
      wx.showToast({
        title: '请选择管辖法院',
        icon: 'none',
        duration: 1000
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    wx.request({
      url: ecfsConfig.IS_PRODUCTION ? (ecfsConfig.SERVER_DOMAIN + ecfsConfig.SELECT_COURT_URL) : (ecfsConfig.SERVER_DOMAIN + ecfsConfig.RAP_URL_PREFIX + ecfsConfig.SELECT_COURT_URL),
      data: {
        cityCode: [],
        countyCode: [],
        provinceCode: []
      },
      method: 'POST',
      success: function (res) {
        if (res.data.code === 200) {
          that.setData({
            courtList: res.data.body.result
          })
        }
      },
      fail: function (res) {
        console.log(res.data);
        console.log('is failed')
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})